package com.commander.gui;

import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.printing.*;
import org.eclipse.swt.widgets.*;

import com.commander.Commander;
import com.commander.gui.messages.InfoMessage;

public class CommanderGUIHandler {
	public static Display display = new Display();
	public static Shell shell = new Shell(CommanderGUIHandler.getDisplay());
	
	public static Composite mainComposite = new Composite(shell, SWT.BORDER);
	public static TabFolder tabFolder = new TabFolder(mainComposite, SWT.BORDER);
	
	public final static TabItem connectTab = new TabItem(tabFolder, SWT.BORDER);
	public final static Composite connectComposite = new Composite(tabFolder, SWT.NONE);
	public final static Label unLabel = new Label(connectComposite, SWT.NULL);
	public final static Text username = new Text(connectComposite, SWT.SINGLE|SWT.BORDER);
	public final static Label pwLabel = new Label(connectComposite, SWT.NULL);
	public final static Text password = new Text(connectComposite, SWT.PASSWORD|SWT.BORDER);
	public final static Button login = new Button(connectComposite, SWT.PUSH);
	public final static List dbList = new List(connectComposite, SWT.SINGLE|SWT.BORDER);
	public final static Button goButton = new Button(connectComposite, SWT.PUSH);
	
	public final static TabItem dbTableTab = new TabItem(tabFolder, SWT.NONE);
	public final static Composite dbTableComposite = new Composite(tabFolder, SWT.BORDER);
	public final static Table dbInfoTable = new Table(dbTableComposite, SWT.SINGLE);
	
	public final static TabItem reportsTab = new TabItem(tabFolder, SWT.NONE);
	public final static Composite reportComposite = new Composite(tabFolder, SWT.BORDER);
	public final static Text reportWindow = new Text(reportComposite, SWT.NONE);
	
	
	public static void initialize(){
		Display.setAppName("Commander");
		Display.setAppVersion("1.0");
		mainComposite.setLayout(new FillLayout(SWT.VERTICAL));
		Rectangle clientArea = shell.getClientArea();
		mainComposite.setBounds(clientArea.x+5, clientArea.y+5, clientArea.width-5, clientArea.height-5);
		//mainComposite.pack();
		shell.setMaximized(true);
		shell.setText("Commander");
		createMenuBar();
		createTabs();
	}
	
	private static void createMenuBar(){
		Menu menu = new Menu(CommanderGUIHandler.getShell(), SWT.BAR);
		Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
		Menu viewMenu = new Menu(CommanderGUIHandler.getShell(), SWT.DROP_DOWN);
		Menu reportMenu = new Menu(CommanderGUIHandler.getShell(), SWT.DROP_DOWN);
		Menu helpMenu = new Menu(CommanderGUIHandler.getShell(), SWT.DROP_DOWN);
		CommanderGUIHandler.getShell().setMenuBar(menu);
		
		MenuItem item = new MenuItem(menu, SWT.CASCADE);
		item.setText("&File");
		item.setMenu(fileMenu);
		
		item = new MenuItem(fileMenu, SWT.PUSH);
		item.setText("&Open");
		item.setAccelerator(SWT.CTRL+'O');
		item.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent event){
				menuOpen();
			}
		});
		
		item = new MenuItem(fileMenu, SWT.PUSH);
		item.setText("&Exit");
		item.setAccelerator(SWT.CTRL+'Q');
		item.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent event){
				Commander.getCommander().exit();
			}
		});
		
		item = new MenuItem(menu, SWT.CASCADE);
		item.setText("&View");
		item.setMenu(viewMenu);
		
		item = new MenuItem(viewMenu, SWT.PUSH);
		item.setText("&Report");
		item.setAccelerator(SWT.CTRL+'R');
		item.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent event){
				openReportViewer();
			}
		});
		
		item = new MenuItem(menu, SWT.CASCADE);
		item.setText("&Help");
		item.setMenu(helpMenu);
		
		item = new MenuItem(helpMenu, SWT.PUSH);
		item.setText("&About");
		item.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent event){
				showAboutBox();
			}
		});
		
//		MenuItem edit = new MenuItem(menu, SWT.CASCADE);
//		edit.setText("&Edit");
//		MenuItem view = new MenuItem(menu, SWT.CASCADE);
//		view.setText("&View");
//		MenuItem report = new MenuItem(menu, SWT.CASCADE);
		
		
	}
	
	protected static void showAboutBox() {
		Shell aboutShell = new Shell(CommanderGUIHandler.getDisplay());
		aboutShell.setText("About:");
		Label label = new Label(aboutShell, SWT.BORDER_SOLID);
		Rectangle clientArea = shell.getClientArea();
		label.setLocation(clientArea.x, clientArea.y);
		label.setText("Commander - MySQL Manipulator Tool (MMT)\n" +
					  "Version 1.0\n"+
					  "Created by Kelly Grogan\n\n"+
					  "This tool is meant to access, edit, and/or create databases on\n"+
					  "the local host using MySQL.  It gives users full control\n"+
					  "over their databases using a graphical user friendly\n"+
					  "environment.");
		label.pack();
		aboutShell.setLocation(500, 300);
		aboutShell.pack();
		aboutShell.open();
		while(!aboutShell.isDisposed())
			if(!CommanderGUIHandler.getDisplay().readAndDispatch())
				CommanderGUIHandler.getDisplay().sleep();
		// TODO Auto-generated method stub
		//new InfoMessage("About is coming soon!");
	}

	protected static void openReportViewer() {
		// TODO Auto-generated method stub
		new InfoMessage("ReportViewer needs to be instantiated!");
	}

	protected static void menuOpen() {
		// TODO need to implement open function
		new InfoMessage("Open function needs to be created!");
	}

//	private void menuPrint(){
//		PrintDialog dialog = new PrintDialog(CommanderGUIHandler.getShell(), SWT.NONE);
//		PrinterData data = dialog.open();
//		if(data == null) return;
//		if(data.printToFile){
//			data.fileName = "print.out";
//		}
//	}
//	
//	private void print(Printer printer){
//		if(printer.startJob("Text")){
//			
//		}
//	}
	
	private static void createTabs(){
		// Set the tab names
		//TabItem conTab = new TabItem(tabFolder, SWT.BORDER);
		new ConnectTab(tabFolder);
		tabFolder.getItems()[0].setText("Connect");
		//createConnectionTab();
		createDBInfoTab();
		createReportTab();
		tabFolder.setVisible(true);
//		connectTab.setText("Connection");
//		reportsTab.setText("Reports");
//		dbTableTab.setText("Database");
		
		//final TabFolder tabFolder = new TabFolder(CommanderGUIHandler.getShell(), SWT.BORDER);
//		Rectangle clientArea = tabFolder.getClientArea();//CommanderGUIHandler.getShell().getClientArea();
//		tabFolder.setLocation(clientArea.x, clientArea.y);
//		connectComposite.setBounds(clientArea.x+10, clientArea.y+10, shell.getSize().x-10, shell.getSize().y-10);
//		connectTab.setControl(connectComposite);
		
		//TabItem primaryTab = new TabItem(tabFolder, SWT.NONE);
		//primaryTab.setText("DB Manipulator");
		//Text text = new Text(connectComposite, SWT.H_SCROLL | SWT.MULTI | SWT.WRAP | SWT.BORDER);
		//connectTab.setControl(text);
		//Rectangle tabArea = connectTab.getClientArea();
		//Rectangle conCompCA = connectComposite.getClientArea();
		//connectComposite.setSize(arg0, arg1)
		//text.setBounds(conCompCA.x+10, conCompCA.y+10, conCompCA.width-10, conCompCA.height-10);
		//text.setSize(800, 800);
		
		
		//tabFolder.pack();
	}
	
	private static void createConnectionTab(){
		GridLayout layout = new GridLayout(2, false);
		connectComposite.setLayout(layout);
		connectTab.setText("Connection");
		Rectangle tabFolderCA = tabFolder.getBounds();
		connectComposite.setBounds(tabFolderCA.x, tabFolderCA.y, tabFolderCA.width-5, tabFolderCA.height-5);
		Rectangle clientArea = connectComposite.getBounds();
		username.setBounds(clientArea.x+60, clientArea.y+15, 50, 20);
		unLabel.setText("Username:");
		unLabel.setBounds(clientArea.x+10, clientArea.y+15, 50, 20);
		password.setBounds(clientArea.x+60, clientArea.y+40, 50, 20);
		pwLabel.setText("Password:");
		pwLabel.setBounds(clientArea.x+10, clientArea.y+10, 50, 20);
		login.setText("Login");
		login.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent event){
				Commander.getCommander().login(username.getText(), password.getText());
			}
		});
		login.setBounds(clientArea.x+10, clientArea.y+65, 50, 20);
		dbList.setBounds(clientArea.x+10, clientArea.y+90, 50, 50);
		goButton.setText("Get Selected DB Data");
		goButton.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent event){
				
			}
		});
		goButton.setBounds(clientArea.x+10, clientArea.y+140, 50, 20);
		
		connectTab.setControl(connectComposite);
		connectComposite.pack();
	}
	
	private static void createDBInfoTab(){
		GridLayout layout = new GridLayout();
		dbTableComposite.setLayout(layout);
		dbTableTab.setText("Data");
		Rectangle tabFolderCA = tabFolder.getClientArea();
		dbTableComposite.setBounds(tabFolderCA.x+10, tabFolderCA.y+5, tabFolderCA.width-10, tabFolderCA.height-5);
		Rectangle clientArea = dbTableTab.getBounds();
		dbInfoTable.setBounds(clientArea.x+10, clientArea.y+50, clientArea.width-10, clientArea.height-10);
		dbTableTab.setControl(reportComposite);
	}
	
	private static void createReportTab(){
		GridLayout layout = new GridLayout();
		reportComposite.setLayout(layout);
		reportsTab.setText("Report");
		Rectangle tabFolderCA = tabFolder.getClientArea();
		reportComposite.setBounds(tabFolderCA.x+5, tabFolderCA.y+5, tabFolderCA.width-5, tabFolderCA.height-5);
		Rectangle clientArea = reportsTab.getBounds();
		reportWindow.setBounds(clientArea.x+10, clientArea.y+10, clientArea.width-10, clientArea.height-10);
		reportsTab.setControl(reportComposite);
	}
	public static Display getDisplay(){return display;}
	public static Shell getShell(){return shell;}
}